// pages/defect/craft/params-options/index.js
import {
  PARAMS_TYPE
} from '../../../../dataSource';
import {
  _update,
  _remove
} from '../../../../api/workmanship-param-type'
const itemList = ['编辑','删除'];
Component({
  /**
   * 组件的属性列表
   */
  properties: {
     
  },

  /**
   * 组件的初始数据
   */
  data: {
    show: false,
    showModal: false,
    dataSource: {} 
  },

  /**
   * 组件的方法列表
   */
  methods: {
    onClose() {
      this.setData({ show: false });
    },
  
    onSelect() {
        this.onModalShow();
    },
    onDelect(){
      wx.showModal({
        title: '是否要删除此参数？',
        success: res=>{
          if(res.confirm){
            _remove(this.data.dataSource, ()=>{
              this.triggerEvent('update')
            })
          }
        }
      })
    },
    open(params,data){
      wx.showActionSheet({
        itemList,
        success: res=>{
          this.setData({
            dataSource: params
          })
          const instance = this.selectComponent('#params');
          instance?.initData(data);
          instance?.setParamsType([PARAMS_TYPE.find(it=> it.value === params.paramType), ...PARAMS_TYPE.filter(it => {
            return data['_params'].find(k => {
              return k['paramType'] === it['value']
            }) === undefined
          })])
          instance?.setDefaultValue({
            vals: [params['paramType']],
            max: params['maxNumber'],
            min: params['minNumber']
          })
          res.tapIndex === 0 && this.onSelect();
          res.tapIndex === 1 && this.onDelect();
        },
        fail(err){
          return
        }
      })
    },
    onModalCancel(){
      this.setData({
        showModal: false
      })
    },
    onModalShow(){
      this.setData({
        showModal: true
      })
    },
    onModalSubmit(event){
      _update({
        ...event.detail,
        id: this.data.dataSource['id']
      },()=>{
        this.triggerEvent('update');
        this.onModalCancel();
      })
    }
  }
})
